//
//  ZFBBusinessViewController.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/1.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBBusinessViewController.h"
#import "ZFBBusinessHeaderBottomView.h"
#import "ZFBBusinessHeaderList.h"
#import "ZFBBusinessHeaderTopView.h"
#import "ZFBLogisticsInfoViewController.h"

@interface ZFBBusinessViewController ()<ZFBBusinessHeaderBottomViewDelegate,ZFBBusinessHeaderTopViewDelegate>

@end

@implementation ZFBBusinessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTableViewAttribute];
    [self setupTableViewHeaderView];
}

-(void)setTableViewAttribute
{
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
}

-(void) setupTableViewHeaderView
{
    CGFloat tableViewWidth = self.tableView.bounds.size.width;
    ZFBBusinessHeaderBottomView *businessHeaderBottomView = [[ZFBBusinessHeaderBottomView alloc]initWithFrame:CGRectMake(0, 0, tableViewWidth, 210)];
    businessHeaderBottomView.delegate = self;
    
    ZFBBusinessHeaderTopView *businessHeaderTopView = [[ZFBBusinessHeaderTopView alloc] initWithFrame:CGRectMake(0, 0, tableViewWidth, tableViewWidth * 0.6)];
    businessHeaderTopView.headerTopListData = [self loadHeaderTopListData];
    businessHeaderTopView.delegate = self;
    CGFloat realH = tableViewWidth / businessHeaderBottomView.bounds.size.width * businessHeaderBottomView.bounds.size.height;
    businessHeaderBottomView.frame = CGRectMake(0, CGRectGetMaxY(businessHeaderTopView.frame), tableViewWidth, realH);
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableViewWidth, CGRectGetMaxY(businessHeaderBottomView.frame))];
    
    [headerView addSubview:businessHeaderTopView];
    [headerView addSubview:businessHeaderBottomView];
    
    self.tableView.tableHeaderView = headerView;
    
}

-(void)businessHeaderBottomView:(ZFBBusinessHeaderBottomView *)businessHeaderBottomView atIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            [self showAlertMsg:@"晨讯"];
            break;
        case 1:
            [self showAlertMsg:@"饰品"];
            break;
        case 2:
            [self showAlertMsg:@"美食"];
            break;
        case 3:
            [self showAlertMsg:@"养生"];
            break;
        default:
            break;
    }
}

-(void)businessHeaderTopView:(ZFBBusinessHeaderTopView *)headerTopView itemTitle:(NSString *)title atIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            [self.navigationController pushViewController:[ZFBLogisticsInfoViewController new] animated:YES];
            break;
        default:
            [self showAlertMsg:title];
            break;
    }
}

-(void)showAlertMsg:(NSString *)msg
{
    [self showAlertWithTitle:@"您当前所选的是:" message:msg preferredStyle:UIAlertControllerStyleAlert actionTitle:@"返回" titleStyle:UIAlertActionStyleCancel handler:nil];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"MyCell %ld",indexPath.row + 1];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}


-(NSArray *)loadHeaderTopListData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"businessType.plist" ofType:nil];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *marr = [NSMutableArray new];
    
    for (NSDictionary *dict in arr) {
        ZFBBusinessHeaderList *bottomList = [ZFBBusinessHeaderList businessHeaderListWithDict:dict];
        [marr addObject:bottomList];
    }
    return marr.copy;
}

@end
